package com.higgins.eventtracking.server;

import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import com.higgins.eventtracking.client.AssetTrackingService;
import com.higgins.eventtracking.client.DietTrackingService;
import com.higgins.eventtracking.client.SecurityAccessDeniedException;
import com.higgins.eventtracking.client.dto.AssetDTO;
import com.higgins.eventtracking.client.dto.FinanceGoalDTO;
import com.higgins.eventtracking.client.dto.InventoryDTO;
import com.higgins.eventtracking.client.dto.MealDTO;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class DietTrackingServiceImpl extends RemoteServiceServlet implements DietTrackingService {
	private static final Logger log = Logger.getLogger(DietTrackingServiceImpl.class.getName());

	@Override
	public List<MealDTO> retrieveMeals() throws IllegalArgumentException {
		DietTrackingDataService dataService = new DietTrackingDataService();
		dataService.openPersistenceManager();
		try	{
			List<MealDTO> meals = dataService.retrieveMeals();
			return meals;
		} catch (Exception e)	{
			log.severe(" " + e.toString());
			return null;
		} finally	{
			dataService.closePersistenceManager();
		}
	}

	@Override
	public Boolean insertConsumption(Date timestamp, String meal, Double calories, Double protein, Double fat, Double water)	{
			DietTrackingDataService dataService = new DietTrackingDataService();
			dataService.openPersistenceManager();
			try	{
				return dataService.insertConsumption(timestamp, meal, calories, protein, fat, water);
			} catch (Exception e)	{
				log.severe(" " + e.toString());
				return false;
			} finally	{
				dataService.closePersistenceManager();
			}
	}
	
   @Override
    public Boolean hasEaten(int hours)    {
            DietTrackingDataService dataService = new DietTrackingDataService();
            dataService.openPersistenceManager();
            try {
                return dataService.hasEaten(hours);
            } catch (Exception e)   {
                log.severe(" " + e.toString());
                return false;
            } finally   {
                dataService.closePersistenceManager();
            }
    }


	@Override
	public Double retrieveCaloriesToday() throws IllegalArgumentException {
		DietTrackingDataService dataService = new DietTrackingDataService();
		dataService.openPersistenceManager();
		try	{
			Double calories = dataService.retrieveCaloriesToday();
			return calories;
		} catch (Exception e)	{
			log.severe(" " + e.toString());
			return null;
		} finally	{
			dataService.closePersistenceManager();
		}
	}

	@Override
	public Double retrieveProteinToday() throws IllegalArgumentException {
		DietTrackingDataService dataService = new DietTrackingDataService();
		dataService.openPersistenceManager();
		try	{
			Double protein = dataService.retrieveProteinToday();
			return protein;
		} catch (Exception e)	{
			log.severe(" " + e.toString());
			return null;
		} finally	{
			dataService.closePersistenceManager();
		}
	}

	@Override
	public Double retrieveFatToday() throws IllegalArgumentException {
		DietTrackingDataService dataService = new DietTrackingDataService();
		dataService.openPersistenceManager();
		try	{
			Double fat = dataService.retrieveFatToday();
			return fat;
		} catch (Exception e)	{
			log.severe(" " + e.toString());
			return null;
		} finally	{
			dataService.closePersistenceManager();
		}
	}

	@Override
	public Double retrieveWaterToday() throws IllegalArgumentException {
		DietTrackingDataService dataService = new DietTrackingDataService();
		dataService.openPersistenceManager();
		try	{
			Double water = dataService.retrieveWaterToday();
			return water;
		} catch (Exception e)	{
			log.severe(" " + e.toString());
			return null;
		} finally	{
			dataService.closePersistenceManager();
		}
	}

	@Override
	public Boolean insertInventory(InventoryDTO dto)
			throws IllegalArgumentException {
		DietTrackingDataService dataService = new DietTrackingDataService();
		dataService.openPersistenceManager();
		try	{
			Boolean result = dataService.insertInventory(dto);
			return result;
		} catch (Exception e)	{
			log.severe(" " + e.toString());
			return false;
		} finally	{
			dataService.closePersistenceManager();
		}
	}
}
